{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitec4538a0ed7a4029b9bd19594323cc7e",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "import _pickle as cPickle\n",
    "import pandas as pd\n",
    "import numpy as np \n",
    "from collections import defaultdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "feture_recommend = cPickle.load(open('./data/feature_recommand.pkl', 'rb'))\n",
    "df_test = cPickle.load(open('./data/df_test.pkl','rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>country</th>\n      <th>age_group</th>\n      <th>isbn</th>\n      <th>rating</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>109814</th>\n      <td>27462</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>1587492695</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>561359</th>\n      <td>137190</td>\n      <td>france</td>\n      <td>3</td>\n      <td>0552997234</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>182843</th>\n      <td>42721</td>\n      <td>canada</td>\n      <td>3</td>\n      <td>0671024108</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>854047</th>\n      <td>208829</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0140503897</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>559003</th>\n      <td>136348</td>\n      <td>usa</td>\n      <td>4</td>\n      <td>0807220299</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1064716</th>\n      <td>257025</td>\n      <td>usa</td>\n      <td>3</td>\n      <td>044022859X</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>427486</th>\n      <td>104636</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0425127583</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1022650</th>\n      <td>246655</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0373706235</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>22450</th>\n      <td>7346</td>\n      <td>usa</td>\n      <td>4</td>\n      <td>0449219399</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>473426</th>\n      <td>115003</td>\n      <td>usa</td>\n      <td>3</td>\n      <td>0316780375</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>344931 rows × 5 columns</p>\n</div>",
      "text/plain": "         user_id country  age_group        isbn  rating\n109814     27462     usa          0  1587492695       0\n561359    137190  france          3  0552997234       8\n182843     42721  canada          3  0671024108       8\n854047    208829  canada          0  0140503897       0\n559003    136348     usa          4  0807220299       9\n...          ...     ...        ...         ...     ...\n1064716   257025     usa          3  044022859X       9\n427486    104636     usa          0  0425127583       0\n1022650   246655     usa          0  0373706235       0\n22450       7346     usa          4  0449219399       0\n473426    115003     usa          3  0316780375       0\n\n[344931 rows x 5 columns]"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "<class 'pandas.core.frame.DataFrame'>\nInt64Index: 344931 entries, 109814 to 473426\nData columns (total 5 columns):\nuser_id      344931 non-null int64\ncountry      344931 non-null object\nage_group    344931 non-null int64\nisbn         344931 non-null object\nrating       344931 non-null int64\ndtypes: int64(3), object(2)\nmemory usage: 15.8+ MB\n"
    }
   ],
   "source": [
    "df_test.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test.country = df_test.country.apply(lambda l: l if l in feture_recommend else 'others')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "usa               230427\ncanada             31331\nothers             26900\nunited kingdom     15239\ngermany            11986\naustralia           7873\nspain               7792\nfrance              4142\nitaly               3665\nportugal            3144\nnew zealand         2432\nName: country, dtype: int64"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test.country.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped = df_test.groupby(['country', 'age_group'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "         '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              2: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              3: {'0156027321',\n               '0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              4: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              5: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'}},\n             'others': {0: {'0060008024',\n               '0316693006',\n               '0316693251',\n               '034536676X',\n               '0380789019',\n               '0385335881',\n               '0385484518',\n               '0385720106',\n               '0439064872',\n               '0439139600',\n               '0446604844',\n               '0446608955',\n               '0553375407',\n               '0670892963',\n               '0671001795',\n               '0679457526',\n               '0688177751',\n               '1558743669',\n               '1573229571',\n               '8826703132'},\n              1: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              2: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              3: {'0061092177',\n               '0099800403',\n               '0151008116',\n               '0312924585',\n               '0345378490',\n               '0345417623',\n               '0439136350',\n               '0439139597',\n               '0441001971',\n               '0446310786',\n               '0515131229',\n               '051513628X',\n               '0553213148',\n               '0553296981',\n               '0590353403',\n               '0671793489',\n               '0747546290',\n               '0786868716',\n               '0842329218',\n               '3257228007'},\n              4: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              5: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'}},\n             'portugal': {0: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              1: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              2: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              3: {'002542730X',\n               '006050918X',\n               '0330262130',\n               '0380815923',\n               '038082101X',\n               '0380977788',\n               '0385413041',\n               '0385721234',\n               '0439139600',\n               '0460877305',\n               '0552137030',\n               '0811214982',\n               '0880381744',\n               '1401201385',\n               '1844262553',\n               '9505156944',\n               '9580464162',\n               '9722113747',\n               '9724614565',\n               '9726081378'},\n              4: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              5: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'}},\n             'spain': {0: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              1: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              2: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              3: {'0156013924',\n               '0452282152',\n               '8408039369',\n               '8420432113',\n               '8420466034',\n               '8420613487',\n               '8420616893',\n               '8420636282',\n               '8423309223',\n               '8433914545',\n               '8440627203',\n               '8445071394',\n               '8445071408',\n               '8445071416',\n               '8445071769',\n               '8478886451',\n               '8478886508',\n               '8495501074',\n               '950491036X',\n               '9681501225'},\n              4: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553',\n               '8408043641'},\n              5: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'}},\n             'united kingdom': {0: {'000649840X',\n               '0099771519',\n               '0156528207',\n               '0330367358',\n               '0330376519',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0552124753',\n               '0590353403',\n               '0590660543',\n               '0618002227',\n               '0671027344',\n               '0749395990',\n               '0749399902',\n               '0771060548',\n               '0877017883',\n               '1844262553',\n               '1857027051'},\n              1: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              2: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              3: {'0064407667',\n               '0091891965',\n               '0140287248',\n               '0140620117',\n               '0192833596',\n               '0330258648',\n               '0330267388',\n               '0380002930',\n               '0380813815',\n               '0440206154',\n               '0440235596',\n               '0552131059',\n               '0552142379',\n               '0552145424',\n               '0552149519',\n               '0671024337',\n               '0722532938',\n               '1844262553',\n               '1857231384',\n               '1860465811'},\n              4: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0552131059',\n               '0552131067',\n               '0552147680',\n               '0552996181',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              5: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '0439139600',\n               '0440219078',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'}},\n             'usa': {0: {'0060248025',\n               '0060256672',\n               '0140185232',\n               '0140309578',\n               '0156004801',\n               '0316735027',\n               '039480029X',\n               '0395977894',\n               '0441002935',\n               '0553148001',\n               '0553213504',\n               '0553351397',\n               '0553380958',\n               '0615116426',\n               '0618002235',\n               '0743455967',\n               '0807083054',\n               '0877738513',\n               '0971942315',\n               '1576738167'},\n              1: {'0064407667',\n               '0156528207',\n               '0385199570',\n               '0439064864',\n               '0439064872',\n               '0439136350',\n               '0439139597',\n               '0552131059',\n               '0590353403',\n               '059035342X',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0812550706',\n               '0877017883',\n               '1844262553'},\n              2: {'0064472272',\n               '0140348107',\n               '0316666343',\n               '0345342968',\n               '0385729332',\n               '0385729340',\n               '0385730586',\n               '0394820371',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '043935806X',\n               '0440237688',\n               '0440439884',\n               '0446608955',\n               '0451191137',\n               '0590353403',\n               '0671027344',\n               '0679886370',\n               '1576738159'},\n              3: {'0060256737',\n               '0060961325',\n               '0060968966',\n               '0066238501',\n               '0140195831',\n               '0316286850',\n               '0316779059',\n               '0375503803',\n               '0394800389',\n               '0395177111',\n               '0451171926',\n               '0451525663',\n               '0517693119',\n               '0553110845',\n               '0670451932',\n               '0805013407',\n               '0890876789',\n               '0898159954',\n               '0920668364',\n               '156402976X'},\n              4: {'0060514957',\n               '0066214769',\n               '0312303467',\n               '0312983220',\n               '0312983301',\n               '0380977311',\n               '0399151451',\n               '0399151613',\n               '0439064864',\n               '0446322180',\n               '0446532452',\n               '0449911519',\n               '0451169514',\n               '0553377868',\n               '0553379615',\n               '0553565079',\n               '0553802461',\n               '0688167888',\n               '0943233828',\n               '1579546463'},\n              5: {'0156528207',\n               '0385199570',\n               '0439064864',\n               '0439136350',\n               '0439139597',\n               '044022165X',\n               '0440234743',\n               '0552131059',\n               '0590353403',\n               '0618002227',\n               '0618129022',\n               '0670032379',\n               '0670894605',\n               '0671027344',\n               '0771060548',\n               '0811801802',\n               '0811825558',\n               '0812550706',\n               '0877017883',\n               '1844262553'}}})"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prepare the feature recommend to improve speed \n",
    "for l in feture_recommend:\n",
    "    for a in feture_recommend[l]:\n",
    "        feture_recommend[l][a] =  set(feture_recommend[l][a].keys())\n",
    "feture_recommend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "('precision', 0.000376870289121386)\n('recall', 0.003228941597924481)\n"
    }
   ],
   "source": [
    "# init counter\n",
    "ac_quantity = 0\n",
    "recommend_quantity = 0 \n",
    "user_fav_quantity = 0\n",
    "for (country, age, uid), g in df_test.groupby(['country', 'age_group', 'user_id']):\n",
    "    # print((country, age, uid))\n",
    "    # print(g)\n",
    "    user_rating = g.groupby('isbn').rating.mean()\n",
    "    # print(user_rating)\n",
    "    # print(set(user_rating[user_rating >= 5]))\n",
    "    user_fav = set(user_rating[user_rating >= 5].index)\n",
    "    # print(user_fav)\n",
    "    # print(feture_recommend[country][age])\n",
    "    user_fav_quantity += len(user_fav)\n",
    "    ac_quantity += len(user_fav & feture_recommend[country][age])\n",
    "    # break\n",
    "recommend_quantity = 20 * len(df_test.user_id.unique())\n",
    "# normalize the equation\n",
    "print(('precision', ac_quantity/ recommend_quantity))\n",
    "print(('recall', ac_quantity/(user_fav_quantity + 1)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "402\n"
    }
   ],
   "source": [
    "print (ac_quantity)"
   ]
  }
 ]
}